package cn.gson.work.controller;

import cn.gson.work.model.pojos.*;
import cn.gson.work.model.service.ResourceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.applet.Applet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ResourceController {

    @Autowired
    ResourceService service;

    //查询资源
    @RequestMapping("/selectResource")
    @ResponseBody
    public Map<String,Object> selectResource(Integer page,Integer size,String resName,String typeName){
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        service.selectResource(resName,typeName,0);
        map.put("rows",objects.getResult());
        map.put("total",objects.getTotal());
        return map;
    }

    //查询资源
    @RequestMapping("/selectResourceTypeId")
    @ResponseBody
    public List<Resource> selectResourceTypeId(Integer typeId){
        List<Resource> resources = service.selectResource("", "", typeId);
        return resources;
    }

    //新增修改资源
    @RequestMapping("/addResource")
    @ResponseBody
    public String addResource(@RequestBody Resource resource){
        try{
            if(resource.getResId()==0){
                resource.setResTime(new Date());
                service.addResource(resource);
            }else{
                service.updateResource(resource);
            }
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //新增修改资源分类
    @RequestMapping("/addType")
    @ResponseBody
    public String addType(@RequestBody Resourcetype resourceType){
        try{
            if(resourceType.getTypeId()==0){
                resourceType.setTypeTime(new Date());
                service.addType(resourceType);
            }else{
                service.updateType(resourceType);
            }
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //删除资源分类
    @RequestMapping("/deleteType")
    @ResponseBody
    public String deleteType(long typeId){
        try{
            service.deleteType(typeId);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    };

    //查询资源分类
    @RequestMapping("/selectType")
    @ResponseBody
    public Map<String,Object> selectType(Integer page,Integer size,String typeName){
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        service.selectType(typeName);
        map.put("rows",objects.getResult());
        map.put("total",objects.getTotal());
        return map;
    }

    @RequestMapping("/selectAllType")
    @ResponseBody
    public List<Resourcetype> selectAllType(){
        List<Resourcetype> resourcetypes = service.selectType("");
        return resourcetypes;
    }

    //新增修改资源申请
    @RequestMapping("/addApp")
    @ResponseBody
    public String addApp(@RequestBody Application application){
        try{
            if(application.getAppId()==0){
                application.setAppTime(new Date());
                application.setAppStage("审核中");
                application.setAppType("资源申请");
                service.addApplication(application);
            }else{
                service.updateApplication(application);
            }
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //新增车辆申请
    @RequestMapping("/addAppCar")
    @ResponseBody
    public String addAppCar(@RequestBody Application application){
        try{
            application.setAppTime(new Date());
            application.setAppStage("审核中");
            application.setAppType("车辆申请");
            service.addAppCar(application);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //查询资源分类
    @RequestMapping("selectAppCar")
    @ResponseBody
    public Map<String,Object> selectAppCar(Integer page,Integer size,String appType,String appStage,String carName){
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        service.selectAppCar(appType,appStage,carName);
        map.put("rows",objects.getResult());
        map.put("total",objects.getTotal());
        return map;
    }

    //查询资源申请
    @RequestMapping("/selectApp")
    @ResponseBody
    public Map<String,Object> selectApp(Integer page,Integer size,String appType,String typeName,String appStage){
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        service.selectApp(appType,typeName,appStage);
        map.put("total",objects.getTotal());
        map.put("rows",objects.getResult());
        return map;
    };

    //新增修改资源申请
    @RequestMapping("/addCar")
    @ResponseBody
    public String addCar(@RequestBody Car car){
        try{
            System.err.println("id:"+car.getCarId());
            if(car.getCarId()==0){
                service.addCar(car);
            }else{
                service.updateCar(car);
            }
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //删除车辆
    @RequestMapping("/deleteCar")
    @ResponseBody
    public String delete(Integer carId){
        try{
            service.deleteCar(carId);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    //查询车辆
    @RequestMapping("/selectCar")
    @ResponseBody
    public Map<String,Object> selectCar(Integer page,Integer size){
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        service.selectCar();
        map.put("total",objects.getTotal());
        map.put("rows",objects.getResult());
        return map;
    };

    //查询车辆
    @RequestMapping("/selectAllCar")
    @ResponseBody
    public List<Car> selectAllCar(){
        List<Car> cars = service.selectCar();
        return cars;
    };

    //查询用户
    @RequestMapping("/selectUser")
    @ResponseBody
    public List<User> selectUser(){
        return service.selectUser();
    };

    //查询部门
    @RequestMapping("/selectDept")
    @ResponseBody
    public List<Dept> selectDept(){
        return service.selectDept();
    };

    //归还资源
    @RequestMapping("/updateAppBack")
    @ResponseBody
    public String updateAppBack(@RequestBody Application application){
        try{
            application.setBackTime(new Date());
            service.updateAppBack(application);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    };

    //归还资源
    @RequestMapping("/updateApprover")
    @ResponseBody
    public String updateApprover(@RequestBody Application application){
        try{
            application.setAppApproverTime(new Date());
            application.setAppApprover("花花");
            service.updateApprover(application);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    };


}
